package com.sihai.ehr.web.action.employee;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.DutyService;
import com.sihai.ehr.service.EmployeeService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.ResignService;
import com.sihai.ehr.web.action.EHRAction;
import com.sihai.ehr.web.pojo.StaffSearchPojo;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.Iterator;
import java.util.List;

@Results({@Result(name = "list", location = "resign_list.jsp"),
        @Result(name = "main", location = "resign_main.jsp")
})
public class ResignAction extends EHRAction {

    @BeanInject
    private ResignService resignService;
    @BeanInject
    private OrganizationService organizationService;
    @BeanInject
    private DutyService dutyService;
    @BeanInject
    private EmployeeService employeeService;

    private LimitedList<EmployeeinfoWork> employeeinfoWorks;

    private List<Organization> childOrgs;
    private List<Organization> organizations;
    private Long currentId;
    private List<Duty> duties;
    private String empIds;
    private StaffSearchPojo search;
    private Long dutyId;
    private String dissmissId;

    @Override
    @Action("/employee/resign_bak")
    public String execute() throws Exception {

        this.listAction = "/employee/resign_list.xhtml";
        this.listTitle = "离职员工资料";
        
        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
            if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        showChildOrg = true;
        // childOrgs = organizationService.findChildren(organizationId, false, null, null);
        // duties = dutyService.searchDuty(null, null, null);
        return "main";
    }

    @Action("/employee/resign_list")
    public String list() throws Exception {

        search = new StaffSearchPojo();
        search.setDepartment(organizationId);
        search.setDuty(dutyId);
        search.setDissmission(dissmissId);
        employeeinfoWorks = resignService.searchResignEmployeeinfo(search, organizationId, "2", createLimit(), createSorter());

        //分页
        this.paging(employeeinfoWorks);
        this.setPageUrl("employee/resign_list.xhtml");
        return "list";
    }

    @Action("/employee/resign_delete")
    public String delete() throws Exception {
        if (StringUtils.isBlank(empIds)) {
            return execute();
        }

        employeeService.removeEmployee(empIds, 1);
        return execute();
    }

    public Long getCurrentId() {
        return currentId;
    }

    public void setCurrentId(Long currentId) {
        this.currentId = currentId;
    }

    public List<Organization> getChildOrgs() {
        return childOrgs;
    }

    public void setChildOrgs(List<Organization> childOrgs) {
        this.childOrgs = childOrgs;
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(List<Organization> organizations) {
        this.organizations = organizations;
    }


    public List<Duty> getDuties() {
        return duties;
    }

    public void setDuties(List<Duty> duties) {
        this.duties = duties;
    }

    public LimitedList<EmployeeinfoWork> getEmployeeinfoWorks() {
        return employeeinfoWorks;
    }

    public void setEmployeeinfoWorks(LimitedList<EmployeeinfoWork> employeeinfoWorks) {
        this.employeeinfoWorks = employeeinfoWorks;
    }

    public String getEmpIds() {
        return empIds;
    }

    public void setEmpIds(String empIds) {
        this.empIds = empIds;
    }

    public StaffSearchPojo getSearch() {
        return search;
    }

    public void setSearch(StaffSearchPojo search) {
        this.search = search;
    }

    public Long getDutyId() {
        return dutyId;
    }

    public void setDutyId(Long dutyId) {
        this.dutyId = dutyId;
    }

    public String getDissmissId() {
        return dissmissId;
    }

    public void setDissmissId(String dissmissId) {
        this.dissmissId = dissmissId;
    }
}
